package com.example.myabtisdemo.controller;

import com.example.myabtisdemo.entity.Student;
import com.example.myabtisdemo.service.impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 前端控制器
 *
 * @author vicente
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/student")
public class StudentController {

  @Autowired private StudentServiceImpl studentService;

  @GetMapping("/all")
  public Object getAll() {
    List<Student> list = studentService.list();
    return list;
  }

  @GetMapping("/AandB")
  public Object AandB() {
    // SELECT id,name,age,sex FROM student WHERE (name = ? AND age = ?)
    List<Student> list =
        studentService.lambdaQuery().eq(Student::getName, "1").eq(Student::getAge, 1).list();
    return list;
  }

  @GetMapping("/AorB")
  public Object AorB() {
    // SELECT id,name,age,sex FROM student WHERE (name = ? OR age = ?)
    List<Student> list =
        studentService.lambdaQuery().eq(Student::getName, "1").or().eq(Student::getAge, 12).list();
    return list;
  }

  @GetMapping("/A_or_CandD")
  public Object A_or_CandD() {
    // SELECT id,name,age,sex FROM student WHERE (name = ? OR (name = ? AND age = ?))
    List<Student> list =
        studentService
            .lambdaQuery()
            .eq(Student::getName, "1")
            .or(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12))
            .list();
    return list;
  }

  @GetMapping("/AandB_or_CandD")
  public Object AandB_or_CandD() {
    // SELECT id,name,age,sex FROM student WHERE ((name = ? AND age = ?) OR (name = ? AND age = ?))
    List<Student> list =
        studentService
            .lambdaQuery()
            .and(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12))
            .or(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12))
            .list();
    return list;
  }

  @GetMapping("/complex")
  public Object complex() {
    // SELECT * FROM student WHERE ((name <> 1) OR (name = 1 AND (age IS NULL OR age >= 11)))
    List<Student> list =
        studentService
            .lambdaQuery()
            .and(wp -> wp.ne(Student::getName, "1"))
            .or(
                wp ->
                    wp.eq(Student::getName, "1")
                        .and(wpp -> wpp.isNull(Student::getAge).or().ge(Student::getAge, 11)))
            .list();
    return list;
  }
}
